﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TableParsers;
using SpeakupBackend.Classes;
using System.ComponentModel;

namespace SpeakupBackend.Themes
{
    [Serializable]
    public partial class GroupTheme: INotifyPropertyChanged, 
                                     IEquatable<GroupTheme>, 
                                     IComparable<GroupTheme>, 
                                     IComparable,
                                     ITheme
    {
        static public readonly ClassType ValidClassType = ClassType.Group;

        public GroupTheme(GroupThemeName name, Level level)
        {
            m_name = name;
            m_level = level;
        }

        public GroupTheme(String name, Level level):
            this(GroupThemeNameParser.Parse(name), level)
        {
        }
        
        private GroupThemeName m_name;
        public GroupThemeName GroupThemeName
        {
            get
            {
                return m_name;
            }
        }

        public String Name
        {
            get
            {
                return m_name.ToString();
            }
        }

        private Level m_level;
        public Level Level
        {
            get
            {
                return m_level;
            }
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as GroupTheme);
        }

        public bool Equals(GroupTheme other)
        {
            return ClassHelper.IEquatableEquals(this, other);
        }

        public override int GetHashCode()
        {
            return ClassHelper.GetHashCode(Name,Level);
        }

        public int CompareTo(Object obj)
        {
            return CompareTo((GroupTheme)obj);
        }

        public int CompareTo(GroupTheme other)
        {
            if (GroupThemeName != other.GroupThemeName)
            {
                return GroupThemeName.CompareTo(other.GroupThemeName);
            }

            return Level.CompareTo(other.Level);
        }

        #region Comparison operators

        static public bool operator ==(GroupTheme rhs, GroupTheme lhs)
        {
            return ClassHelper.OperatorEqual(rhs, lhs);
        }

        static public bool operator !=(GroupTheme rhs, GroupTheme lhs)
        {
            return ClassHelper.OperatorNotEqual(rhs, lhs);
        }

        static public bool operator >(GroupTheme rhs, GroupTheme lhs)
        {
            return rhs.CompareTo(lhs) > 0;
        }

        static public bool operator <(GroupTheme rhs, GroupTheme lhs)
        {
            return rhs.CompareTo(lhs) < 0;
        }

        static public bool operator >=(GroupTheme rhs, GroupTheme lhs)
        {
            return rhs.CompareTo(lhs) >= 0;
        }

        static public bool operator <=(GroupTheme rhs, GroupTheme lhs)
        {
            return rhs.CompareTo(lhs) <= 0;
        }

        #endregion
    }

    public partial class GroupTheme : ThemeBase
    {
        public override bool IsCompatibleClass(Class cl)
        {
            //bad unit
            if (cl.Unit < Level.MinValidUnitNumber || cl.Unit > Level.MaxValidUnitNumber)
            {
                return false;
            }

            Level classLevel = Level.GetLevelByUnit(cl.Unit);
            
            GroupThemeName classGroupThemeName;
            if (!GroupThemeNameParser.TryParse(cl.Name.Name, out classGroupThemeName))
            {
                return false;
            }

            return cl.Name.Type == ClassType.Group &&
                   classGroupThemeName == this.GroupThemeName &&
                   classLevel == this.Level;
        }
        
    }
}
